package com.cn.zpark;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @PackageName: com.cn.zpark
 * @ClassName: ServletContextDemo02
 * @Description:
 * @author: RZS
 * @date: 2022/4/8  10:59
 */
@WebServlet("/scd2.do")
public class ServletContextDemo02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 1、获取上下文对象
         * 2、根据上下文对象获取上下文初始化参数
         * 3、将参数值 + 1
         * 4、将访问该网站的次数响应到浏览器
         */
        // 1、获取上下文对象
        ServletContext context = request.getServletContext();
        // 2、根据上下文对象获取上下文初始化参数
        String count = context.getInitParameter("count");
        // 将count转为int
        int number = Integer.parseInt(count);
        // 3、将参数值 + 1
        ++ number;
        // 4、将访问该网站的次数响应到浏览器
        response.setContentType("text/html;charset=utf-8"); // 设置响应格式
        // 获取写出对象流
        PrintWriter writer = response.getWriter();
        writer.println("<h1>该网站被浏览的次数为： " + number + "次</h1>");
        writer.close();
    }
}
